package com.fita.admin.entity.search.order;

import com.demo2do.core.persistence.support.QueryFilter;
import com.fita.admin.entity.search.AbstractSearch;
import com.fita.entity.order.reverse.ReverseOrderStatus;
import com.fita.entity.order.reverse.ReverseOrderType;

/**
 * @description:
 * @author: 11200
 * @date: 2019/8/22
 */
public class ReverseOrderSearch extends AbstractSearch {

    private ReverseOrderType type;

    private ReverseOrderStatus status;

    private boolean needPay;

    private String orderCode;

    private String mobile;

    /**
     * get prepared filter
     *
     * @param refresh whether to re-create filter
     * @return
     */
    @Override
    public QueryFilter getPreparedFilter(boolean refresh) {

        if (refresh || this.filter == null){
            this.filter = new QueryFilter();

            this.filter.addEquals("type", type);

            if (needPay){

                this.setStatus(null);

                this.filter.addSentence("(price is not null and actualPayPrice is not null and price - actualPayPrice > 0 and 1 = :n and status in ('TICKETED', 'WAITING_FOR_TICKET'))", "n", 1);
            }else {
                this.filter.addEquals("status", status);
            }
            this.filter.addEquals("code", orderCode);

            this.filter.addEquals("agent.mobile", mobile);

        }
        return this.filter;
    }

    /**
     * build query string template (sql or hql with :keys)
     *
     * @return
     */
    @Override
    public String buildSentenceTemplate() {
        return "FROM ReverseOrder WHERE disabled = false " + getPreparedFilter(false).clauseSentence();
    }

    public ReverseOrderType getType() {
        return type;
    }

    public void setType(ReverseOrderType type) {
        this.type = type;
    }

    public ReverseOrderStatus getStatus() {
        return status;
    }

    public void setStatus(ReverseOrderStatus status) {
        this.status = status;
    }

    public boolean isNeedPay() {
        return needPay;
    }

    public void setNeedPay(boolean needPay) {
        this.needPay = needPay;
    }

    public String getOrderCode() {
        return orderCode;
    }

    public void setOrderCode(String orderCode) {
        this.orderCode = orderCode;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }
}
